package com.exam.sacrifice.ui;

import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.sacrifice.R;
import com.exam.sacrifice.bean.GongPinInfo;
import com.exam.sacrifice.databinding.ActivityGongPinBinding;
import com.exam.sacrifice.ui.adapter.GongPinAdapter;
import com.mt.mtloadingmanager.LoadingDialog;

public class GongPinActivity extends BaseActivity<BaseViewModel, ActivityGongPinBinding> {

    private GongPinAdapter mGongPinAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gong_pin;
    }

    @Override
    public void initData() {

        binding.include.tvTitle.setText("贡品");
        binding.include.ivback.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                back();
            }
        });

        binding.tvMenu1.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                initGongPin(0);
            }
        });

        binding.tvMenu2.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                initGongPin(1);
            }
        });

        binding.tvMenu3.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                initGongPin(2);
            }
        });

        mGongPinAdapter = new GongPinAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        binding.recyclerView.setLayoutManager(gridLayoutManager);
        binding.recyclerView.setAdapter(mGongPinAdapter);
        mGongPinAdapter.setOnItemClickListener(new GongPinAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(GongPinInfo item) {
                loadingManager.show("购买中");
                binding.recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadingManager.hideSuccess(String.format("成功购买%d个【%s】", item.buyCount, item.name), new LoadingDialog.OnDialogDismissedListener() {
                            @Override
                            public void onDialogDismissed() {
                                finish();
                            }
                        });
                    }
                }, 1500);
            }
        });

        initGongPin(0);
    }

    private void initGongPin(int type) {

        binding.tvMenu1.setTextColor(ContextCompat.getColor(context, com.exam.commonbiz.R.color.color_333333));
        binding.tvMenu2.setTextColor(ContextCompat.getColor(context, com.exam.commonbiz.R.color.color_333333));
        binding.tvMenu3.setTextColor(ContextCompat.getColor(context, com.exam.commonbiz.R.color.color_333333));

        mGongPinAdapter.getData().clear();
        if (type == 0) {
            mGongPinAdapter.addData(new GongPinInfo("花束1", R.drawable.hua_a));
            mGongPinAdapter.addData(new GongPinInfo("花束2", R.drawable.hua_b));
            mGongPinAdapter.addData(new GongPinInfo("花束3", R.drawable.hua_c));
            mGongPinAdapter.addData(new GongPinInfo("花束4", R.drawable.hua_d));
            binding.tvMenu1.setTextColor(ContextCompat.getColor(context, R.color.main_color));
        } else if (type == 1) {
            mGongPinAdapter.addData(new GongPinInfo("香烛1", R.drawable.xianghzhu_a));
            mGongPinAdapter.addData(new GongPinInfo("香烛2", R.drawable.xiangzhu_b));
            binding.tvMenu2.setTextColor(ContextCompat.getColor(context, R.color.main_color));
        } else if (type == 2) {
            mGongPinAdapter.addData(new GongPinInfo("香蕉", R.drawable.shuiguo_e));
            mGongPinAdapter.addData(new GongPinInfo("橙子", R.drawable.shuiguo_a));
            mGongPinAdapter.addData(new GongPinInfo("红枣", R.drawable.shuiguo_f));
            binding.tvMenu3.setTextColor(ContextCompat.getColor(context, R.color.main_color));
        }
    }
}